/**
 * 用户管理
 * 
 * @class AppUserView
 * @extends Ext.Panel
 */

Ext.define('AppUserView', {
	extend : 'Ext.panel.Panel',
	id : 'AppUserView',
	title : '账号信息',
	iconCls : 'menu-appuser',
	layout : 'border',

	initComponent : function() {
		// 搜索
		this.searchPanel = new KE.SearchPanel({
					region : 'north',
					layout : {
						type : 'hbox',
						align : 'middle'
					},
					defaults : {
						xtype : 'label',
						style : 'margin-left:10px;'
					},
					items : [{
								text : '账号'
							}, {
								xtype : 'textfield',
								name : 'Q_LIKES_username',
								width : 120
							}, {
								text : '姓名'
							}, {
								xtype : 'textfield',
								name : 'Q_LIKES_fullname',
								width : 120
						}	, {
								xtype : 'button',
								text : '查询',
								scope : this,
								iconCls : 'btn-search',
								handler : this.search
							}, {
								xtype : 'button',
								text : '清空',
								scope : this,
								iconCls : 'reset',
								handler : this.reset
							}]
				});
		// Grid的工具栏
		this.toolbar = new Ext.Toolbar({
					items : []
				});
		if (isGranted('_AppUserAdd')) {
			this.toolbar.add(new Ext.Button({
						text : '添加人员',
						iconCls : 'add-user',
						handler : function() {
							App.clickTopTab('UserFormPanel');
						}
					}));
		}
		if (isGranted('_AppUserDel')) {
			if (isGranted('_AppUserAdd')) {
				this.toolbar.add('-');
			}
			this.toolbar.add(new Ext.Button({
				iconCls : 'btn-del',
				text : '删除人员',
				scope : this,
				handler : function() {
					var grid = Ext.getCmp("AppUserGrid");

					var selectRecords = grid.getSelectionModel().getSelection();
					if (selectRecords.length == 0) {
						Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
						return;
					}
					var ids = Array();
					var idsN = '';
					for (var i = 0; i < selectRecords.length; i++) {
						if (selectRecords[i].data.userId != 1) {
							ids.push(selectRecords[i].data.userId);
						} else {
							idsN += selectRecords[i].data.fullname + ',';
						}
					}
					if (idsN == '') {
						this.remove(ids);
					} else {
						Ext.ux.Toast.msg("信息", idsN + "不能被删除！");
					}
				}
			}));
		}
		this.toolbar.add('-');
		this.toolbar.add(new Ext.Button({
					iconCls : 'btn-refresh',
					text : '刷新',
					scope : this,
					handler : function() {
						this.gridPanel.getStore().load();
					}
				}));

		// Grid表格
		this.gridPanel = new KE.GridPanel({
			region : 'center',
			tbar : this.toolbar,
			id : 'AppUserGrid',
			url : __ctxPath + '/system/listAppUser.do',
			fields : [{
						name : 'userId',
						type : 'int'
					}, 'username', 'password', 'fullname', 'address', 'email',
					'depNames', // 'title',// 性别
					'posNames', 'roleNames', 'dynamicPwd', 'dyPwdStatus', {
						name : 'accessionTime'
					}, 'status', 'userCode'],
			columns : [{
						header : 'userId',
						dataIndex : 'userId',
						hidden : true
					}, {
						header : '状态',
						dataIndex : 'status',
						flex: 0.3,
						renderer : function(value) {
							var str = '';
							if (value == '1') {// 激活用户
								str += '<img title="激活" src="'
										+ __ctxPath
										+ '/images/flag/customer/effective.png"/>'
							} else {// 禁用用户
								str += '<img title="禁用" src="'
										+ __ctxPath
										+ '/images/flag/customer/invalid.png"/>'
							}
							return str;
						}
					}, {
						header : '账号',
						dataIndex : 'username',
						width : 60,
						isExp : true
					}, {
						header : '地址',
						dataIndex : 'address',
						hidden : true,
						exprint : true
					}, {
						header : '姓名',
						dataIndex : 'fullname',
						width : 60
					}, {// 先不显示
						header : '所属组织',
						dataIndex : 'depNames',
						sortable : false,
						width : 60
					}, {
						header : '拥有角色',
						dataIndex : 'roleNames',
						sortable : false,
						width : 60
					}, {
						header : '管理',
						dataIndex : 'userId',
						xtype : 'actioncolumn',
						width : 60,
						items : [{
									iconCls : 'btn-del',
									tooltip : '删除',
									scope : this,
									handler : function(grid, rowIndex, colIndex) {
										var record = grid.getStore()
												.getAt(rowIndex);
										var editId = record.data.userId;
										this.remove(editId);
									}
								}, {
									iconCls : 'btn-edit',
									tooltip : '编辑',
									scope : this,
									handler : function(grid, rowIndex, colIndex) {
										var record = grid.getStore()
												.getAt(rowIndex);
										var editId = record.data.userId;
										var editName = record.data.username;
										this.edit(editId, editName);
									}
								}, {
									iconCls : 'btn-password',
									tooltip : '重置',
									scope : this,
									handler : function(grid, rowIndex, colIndex) {
										var record = grid.getStore()
												.getAt(rowIndex);
										var editId = record.data.userId;
										this.resetPwd(editId);
									}
								}]
					}]
		});
		this.items = [this.searchPanel, this.gridPanel];
		this.callParent();
		this.gridPanel.on('rowdblclick', this.rowdblclick, this);
	},

	// 重置查询表单
	reset : function() {
		this.searchPanel.getForm().reset();
		Ext.each(this.searchPanel.query('combo'), function(c) {
					c.setValue('');
				});
	},
	// 按条件搜索
	search : function() {
		$search({
					searchPanel : this.searchPanel,
					gridPanel : this.gridPanel
				});
	},
	rowdblclick : function(grid, record) {
		var userId = record.data.userId;
		if (isGranted('_AppUserEdit') && userId != 1) {
			this.edit(userId, record.data.username);
		}
	},
	// 用户删除
	remove : function(_ids) {
		Ext.Msg.confirm('删除操作', '你确定要删除该用户吗?', function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
									url : __ctxPath
											+ '/system/multiDelAppUser.do',
									method : 'post',
									params : {
										ids : _ids
									},
									success : function(response) {
										var result = Ext.util.JSON
												.decode(response.responseText);
										if (result.msg == '') {
											Ext.ux.Toast.msg("操作信息", "用户删除成功");
										} else {
											Ext.ux.Toast
													.msg("操作信息", result.msg);
										}
										Ext.getCmp('AppUserGrid').getStore()
												.reload();
									},
									failure : function() {
										Ext.ux.Toast.msg("操作信息", "用户删除失败");
									}
								});
					}
				});
	},
	// 重置密码
	resetPwd : function(userId) {
		new setPasswordForm(userId);
	},
	// 用户编辑
	edit : function(userId, username) {
		App.clickTopTab('UserFormPanel_' + userId, {
					userId : userId,
					username : username
				});
	}

});
